﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.HYSpace
{
	public partial class FormEstimateLoadShow : System.Windows.Forms.Form
	{
		public FormEstimateLoadShow(List<LoadEstimateParam> listLoadParam, UIDocument uiDocument)
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.listLoad = listLoadParam;
			this.uiDoc = uiDocument;
			this.estimateOperate = new EstimateLoadOperate(this.uiDoc);
		}

		private void FormEstimateLoadShow_Load(object sender, EventArgs e)
		{
			this.dataGridView1.DataSource = this.listLoad;
			for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
			{
				if (i % 2 != 0)
				{
                    this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Yellow;
				}
			}
		}

		private void btnExcel_Click(object sender, EventArgs e)
		{
			this.estimateOperate.ExportExcel(this.listLoad);
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
			base.Close();
		}

		private void btnMark_Click(object sender, EventArgs e)
		{
			this.estimateOperate.MarkText(this.listLoad);
		}

		private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
		{
			if (!this.beCheck || e.RowIndex < 0)
			{
				return;
			}
			string pattern = "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
			if (this.dataGridView1.Columns[e.ColumnIndex].Name == "ColHeat" || this.dataGridView1.Columns[e.ColumnIndex].Name == "ColCool")
			{
				bool flag = e.FormattedValue == null;
				bool flag2 = !Regex.IsMatch(e.FormattedValue.ToString(), pattern);
				if (flag || flag2)
				{
					YJKMessageBox.Information("指标值大于零的数字，\n小数时仅支持两位小数！");
					e.Cancel = true;
				}
			}
		}

		private void dataGridView1_MouseEnter(object sender, EventArgs e)
		{
			this.beCheck = true;
		}

		private void dataGridView1_MouseLeave(object sender, EventArgs e)
		{
			this.beCheck = false;
		}

		private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
		{
			if (!this.beCheck || e.RowIndex < 0 || e.ColumnIndex < 0)
			{
				return;
			}
			string pattern = "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
			if ((this.dataGridView1.Columns[e.ColumnIndex].Name == "ColHeat" || this.dataGridView1.Columns[e.ColumnIndex].Name == "ColCool") && (this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null || !Regex.IsMatch(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), pattern)))
			{
				YJKMessageBox.Information("指标值大于零的数字，\n小数时仅支持两位小数！");
				this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
			}
			try
			{
				int num = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells["ColElemID"].Value);
				LoadEstimateParam loadEstimateParam = null;
				foreach (LoadEstimateParam loadEstimateParam2 in this.listLoad)
				{
					if (loadEstimateParam2.ElemId == num)
					{
						loadEstimateParam = loadEstimateParam2;
					}
				}
				if (loadEstimateParam != null)
				{
					if (this.dataGridView1.Columns[e.ColumnIndex].Name == "ColHeat")
					{
						double heatIndex = Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
						loadEstimateParam.HeatIndex = heatIndex;
						this.dataGridView1.Rows[e.RowIndex].Cells["ColTotalHeat"].Value = loadEstimateParam.HeatLoad;
						this.dataGridView1.Refresh();
					}
					if (this.dataGridView1.Columns[e.ColumnIndex].Name == "ColCool")
					{
						double coolIndex = Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
						loadEstimateParam.CoolIndex = coolIndex;
						this.dataGridView1.Rows[e.RowIndex].Cells["ColTotalCool"].Value = loadEstimateParam.CoolLoad;
						this.dataGridView1.Refresh();
					}
				}
			}
			catch (Exception)
			{
				throw;
			}
		}

		private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
		{
			YJKMessageBox.Warning("请输入正数！");
			e.Cancel = true;
		}

		private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
		{
		}

		private List<LoadEstimateParam> listLoad;

		private UIDocument uiDoc;

		private EstimateLoadOperate estimateOperate;

		private bool beCheck;
	}
}
